Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Graphics
Imports FMPLib01

Public Class FmpDrawingSurface
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlSurface As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.pnlSurface = New System.Windows.Forms.Panel()
        Me.SuspendLayout()
        '
        'pnlSurface
        '
        Me.pnlSurface.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlSurface.BackColor = System.Drawing.SystemColors.Control
        Me.pnlSurface.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlSurface.Location = New System.Drawing.Point(8, 8)
        Me.pnlSurface.Name = "pnlSurface"
        Me.pnlSurface.Size = New System.Drawing.Size(272, 176)
        Me.pnlSurface.TabIndex = 0
        '
        'FmpDrawingSurface
        '
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.pnlSurface})
        Me.Name = "FmpDrawingSurface"
        Me.Size = New System.Drawing.Size(288, 192)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public fmpFlexMap As New FlexMapData()
    Public selectedGraphicsPath As GraphicsPath
    Public pathSelected As Boolean = False

    Public Event Selected(ByVal b As Boolean)
    Public Event SurfaceMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
    Public Event Rendered()

    Private ZControl As zoomControl

    Public Sub DrawPath(ByRef pth As GraphicsPath)
        Dim gr As Graphics = pnlSurface.CreateGraphics

        Dim p As New Pen(Color.WhiteSmoke)

        If Not ZControl Is Nothing Then
            ZControl.ApplyTransforms(gr)

        End If

        Try
            gr.DrawPath(p, pth)
        Catch
        Finally
            gr.Dispose()
        End Try
    End Sub

    Public Sub DeselectGraphicsPath(Optional ByVal withRender As Boolean = True)
        pathSelected = False
        selectedGraphicsPath = Nothing
        If withRender Then Me.Invalidate()

    End Sub

    Public Sub SelectGraphicsPath(ByVal e As System.Windows.Forms.MouseEventArgs)
        ' do a hit test for each item until it is found
        Dim feat As Feature
        Dim i As Int16

        If fmpFlexMap.Features.Count < 2 Then
            pathSelected = False
            Exit Sub
        End If

        For i = (fmpFlexMap.Features.Count - 1) To 1 Step -1
            Dim pth As GraphicsPath
            feat = fmpFlexMap.Features(i)
            For Each pth In feat.graphPaths
                Dim tempReg As New Region()
                tempReg.Intersect(pth)
                If tempReg.IsVisible(e.X, e.Y) Then
                    pathSelected = True
                    selectedGraphicsPath = pth
                    RaiseEvent Selected(True)
                    Me.Invalidate()
                    Exit Sub
                Else
                    pathSelected = False
                    DeselectGraphicsPath(False)
                    RaiseEvent Selected(False)
                    Me.Invalidate()
                End If
            Next
        Next

    End Sub

    Public Sub DeleteSelected()
        If pathSelected = True Then
            ' do a second check
            If Not selectedGraphicsPath Is Nothing Then
                Dim feat As Feature
                Dim pth As GraphicsPath
                For Each feat In fmpFlexMap.Features
                    For Each pth In feat.graphPaths
                        If pth Is selectedGraphicsPath Then
                            feat.graphPaths.Remove(pth)
                            pathSelected = False
                            RaiseEvent Selected(False)
                            Me.Invalidate()
                            Exit Sub
                        End If
                    Next
                Next
            End If
        End If
    End Sub

    Private Sub Render()
        Dim gr As Graphics = pnlSurface.CreateGraphics
        gr.Clear(Color.Black)

        If Not ZControl Is Nothing Then
            ZControl.ApplyTransforms(gr)
        End If

        If fmpFlexMap Is Nothing Then Exit Sub

        Try
            fmpFlexMap.Render(gr, gr.VisibleClipBounds)
            If pathSelected = True Then
                Try
                    gr.DrawPath(Pens.Yellow, selectedGraphicsPath)
                Catch ex As Exception
                    Debug.WriteLine("Error!! " & ex.Message)
                End Try
            End If
            Debug.WriteLine("Successful Render")
            RaiseEvent Rendered()
        Catch ex As Exception
            Debug.WriteLine("Error!! " & ex.Message)
        Finally
            gr.Dispose()
        End Try
    End Sub

    Public Sub RenderFMP()
        Render()
    End Sub

    Private Sub pnlSurface_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlSurface.MouseDown
        RaiseEvent SurfaceMouseDown(e)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Render()

    End Sub

    Public WriteOnly Property ZoomControl() As ZoomControl
        Set(ByVal Value As ZoomControl)
            ZControl = Value
        End Set
    End Property


End Class
